package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.service.FriendsService;
import com.copyctrl.model.CampFriends;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/friend")
public class FriendsController {
    @Autowired
    FriendsService service;

    @GetMapping("/all")
    public ApiResponse getAll(){
        return ApiResponse.ofSuccess(service.findAll());
    }

    @GetMapping("/{id}")
    public ApiResponse getById(@PathVariable int id){
        return ApiResponse.ofSuccess(service.findById(id));
    }

    @PostMapping("/{json}")
    public ApiResponse add(@PathVariable String json){
        CampFriends campFriends = JSONUtil.toBean(json, CampFriends.class);
        campFriends.setTime(new Date());
        return ApiResponse.ofSuccess(service.add(campFriends));
    }
    @DeleteMapping("/{id}")
    public ApiResponse delete(@PathVariable int id){
        return ApiResponse.ofSuccess(service.delete(id));
    }
    @PutMapping("/{json}")
    public ApiResponse update(@PathVariable String json){
        CampFriends campFriends = JSONUtil.toBean(json, CampFriends.class);
        return ApiResponse.ofSuccess(service.update(campFriends));
    }
}
